
clear
set more off
capture log close
set seed 12345
set matsize 10000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"

* set DD year
local ddyear "2016"



**
** prep top firm indicator
**
use $DATA/reg_file_firm.dta, clear

keep firm_tin year t1_2012     
sort firm_tin year
tempfile tt1
save `tt1', replace

****************************
**  Prep individual data - stayers, turnover, stayers by baseline position
****************************
use $DATA/scorp100p_entsep_file, clear
local yy "`ddyear'"
keep if year==2012 | year==2011 | year==`yy' | year==`yy'-1
tab year

* prep entrants, separators, stayers
sort firm_tin tin year
gen zz = year==`yy'
by firm_tin tin: egen c`yy' = max(zz)
drop zz
gen zz = year==2011
by firm_tin tin: egen c2011 = max(zz)
drop zz
gen stay = c2012==1 & c`yy'==1
tab year stay

sort firm_tin year
merge m:1 firm_tin year using `tt1'
tab year _merge
keep if _merge==3
drop _merge

gen wagecat = 1 if wages<5870
replace wagecat = 2 if wages>=5870 
replace wagecat = 3 if wages>=8760
replace wagecat = 4 if wages>=12610
replace wagecat = 5 if wages>=17340
replace wagecat = 6 if wages>=22900
replace wagecat = 7 if wages>=29380
replace wagecat = 8 if wages>=37130 
replace wagecat = 9 if wages>=47950
replace wagecat = 10 if wages>=67590
replace wagecat = 11 if wages>=89640 
replace wagecat = 12 if wages>=160830
replace wagecat = . if wages==.
tab wagecat if ft==1 & year==2012 & t1_2012==1
local wc "12"

gen wages_ft = 0
replace wages_ft = wages if ft==1
bys firm_tin year: egen wages_tot = sum(wages)
bys firm_tin year: egen wages_ft_tot = sum(wages_ft) 

*save intermediate dataset to be deleted at end of program
compress
save zzfinish_del, replace


***************************************************************
***************************************************************
***************************************************************
**  Panel A - Turnover 
***************************************************************
***************************************************************
***************************************************************
use zzfinish_del, clear
local wc "12"
keep if stay==0
local ssamp "turn"

keep tin ft wages wages_ft  wages_tot wages_ft_tot firm_tin year wagecat
collapse (count) n_cat=tin (sum) n_catft=ft wages wages_ft (mean) wages_tot wages_ft_tot , by(firm_tin year wagecat)

sort firm_tin year
foreach o in wages wages_ft n_catft n_cat {
	forval i=1/`wc' {
		gen zz = 0
		replace zz = `o' if wagecat==`i'
		by firm_tin year: egen `o'`i'_`ssamp' = sum(zz)
		drop zz
	}
	by firm_tin year: egen `o'_tot_`ssamp' = sum(`o')
}
bys firm_tin year: gen nf=_n==1
keep if nf==1

sort firm_tin year
foreach o in wages wages_ft {
	forval i=1/`wc' {
		gen zz = 0 
		replace zz = `o'`i'_`ssamp' if year==2012
		by firm_tin: egen `o'`i'_`ssamp'_2012 = sum(zz)
		drop zz
	}
	gen zz = 0 
	replace zz = `o'_tot_`ssamp' if year==2012
	by firm_tin: egen `o'_`ssamp'_tot2012 = sum(zz)
	drop zz	
}
drop n_cat n_catft wages wages_ft wages_tot wages_ft_tot wagecat nf

*merge on distrib information from full sample
sort firm_tin year
merge 1:1 firm_tin year using $DATA/wage_distrib_all.dta
tab year _merge
gen flag_ind_`ssamp' = _merge==3
drop _merge

foreach vv in wages wages_ft n_cat n_catft {
forval i=1/12 {
	replace `vv'`i'_`ssamp' = 0 if `vv'`i'_`ssamp'==.
}
}

foreach vv in wages wages_ft {
forval i=1/12 {
	replace `vv'`i'_`ssamp'_2012 = 0 if `vv'`i'_`ssamp'_2012==.
}
}

foreach vv in n_cat_tot_`ssamp'  n_catft_tot_`ssamp'  wages_tot_`ssamp'  wages_ft_tot_`ssamp' wages_ft_`ssamp'_tot2012  wages_`ssamp'_tot2012 {
	replace `vv' = 0 if `vv'==.
}

* net shares
foreach o in wages_ft {
	forval i=1/`wc' {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

* create bins for deciles for main regs
forval j = 1/9 {
	gen dec`j'_`ssamp' = wages_ft`j'_`ssamp' 
	gen dec`j'_`ssamp'_2012 = wages_ft`j'_`ssamp'_2012 
}
egen dec10_`ssamp' = rowtotal(wages_ft10_`ssamp'-wages_ft12_`ssamp')
egen dec10_`ssamp'_2012 = rowtotal(wages_ft10_`ssamp'_2012-wages_ft12_`ssamp'_2012)
foreach o in dec {
	forval i=1/10 {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

*************
**  Regressions
****************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.owntot_2012cat##ib2012.year i.naics_`baseyr'##ib`baseyr'.year i.state_2012##ib`baseyr'.year"

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t2011 t2015 t2016 y2011 y2015 y2016 `t'"

foreach r in 1 {
	forval j=1/9 {
		eststo r`r'_w`j': quietly areg dptn_dec`j'_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & n_ft_lower_2012>=15 , absorb(firm_tin) vce(cluster firm_tin)
	}
	eststo r`r'_w10: quietly areg dptn_dec10_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & tt==0 & n_ft_lower_2012>=15, absorb(firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A6_panelA_`ssamp'.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear



***************************************************************
***************************************************************
***************************************************************
**  Panel A - Stayers 
***************************************************************
***************************************************************
***************************************************************
use zzfinish_del, clear
local wc "12"
keep if stay==1
local ssamp "stay"


keep tin ft wages wages_ft  wages_tot wages_ft_tot firm_tin year wagecat
collapse (count) n_cat=tin (sum) n_catft=ft wages wages_ft (mean) wages_tot wages_ft_tot , by(firm_tin year wagecat)

sort firm_tin year
foreach o in wages wages_ft n_catft n_cat {
	forval i=1/`wc' {
		gen zz = 0
		replace zz = `o' if wagecat==`i'
		by firm_tin year: egen `o'`i'_`ssamp' = sum(zz)
		drop zz
	}
	by firm_tin year: egen `o'_tot_`ssamp' = sum(`o')
}
bys firm_tin year: gen nf=_n==1
tab year nf
keep if nf==1

sort firm_tin year
foreach o in wages wages_ft {
	forval i=1/`wc' {
		gen zz = 0 
		replace zz = `o'`i'_`ssamp' if year==2012
		by firm_tin: egen `o'`i'_`ssamp'_2012 = sum(zz)
		drop zz
	}
	gen zz = 0 
	replace zz = `o'_tot_`ssamp' if year==2012
	by firm_tin: egen `o'_`ssamp'_tot2012 = sum(zz)
	drop zz	
}
drop n_cat n_catft wages wages_ft wages_tot wages_ft_tot wagecat nf

*merge on distrib information from full sample
sort firm_tin year
merge 1:1 firm_tin year using $DATA/wage_distrib_all.dta
tab year _merge
gen flag_ind_`ssamp' = _merge==3
drop _merge

foreach vv in wages wages_ft n_cat n_catft {
forval i=1/12 {
	replace `vv'`i'_`ssamp' = 0 if `vv'`i'_`ssamp'==.
}
}

foreach vv in wages wages_ft {
forval i=1/12 {
	replace `vv'`i'_`ssamp'_2012 = 0 if `vv'`i'_`ssamp'_2012==.
}
}

foreach vv in n_cat_tot_`ssamp'  n_catft_tot_`ssamp'  wages_tot_`ssamp'  wages_ft_tot_`ssamp' wages_ft_`ssamp'_tot2012  wages_`ssamp'_tot2012 {
	replace `vv' = 0 if `vv'==.
}

* net shares
foreach o in wages_ft {
	forval i=1/`wc' {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

* create bins for deciles for main regs
forval j = 1/9 {
	gen dec`j'_`ssamp' = wages_ft`j'_`ssamp' 
	gen dec`j'_`ssamp'_2012 = wages_ft`j'_`ssamp'_2012 
}
egen dec10_`ssamp' = rowtotal(wages_ft10_`ssamp'-wages_ft12_`ssamp')
egen dec10_`ssamp'_2012 = rowtotal(wages_ft10_`ssamp'_2012-wages_ft12_`ssamp'_2012)
foreach o in dec {
	forval i=1/10 {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

*************
**  Regressions
****************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.owntot_2012cat##ib2012.year i.naics_`baseyr'##ib`baseyr'.year i.state_2012##ib`baseyr'.year"

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t2011 t2015 t2016 y2011 y2015 y2016 `t'"

foreach r in 1 {
	forval j=1/9 {
		eststo r`r'_w`j': quietly areg dptn_dec`j'_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & n_ft_lower_2012>=15 , absorb(firm_tin) vce(cluster firm_tin)
	}
	eststo r`r'_w10: quietly areg dptn_dec10_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & tt==0 & n_ft_lower_2012>=15, absorb(firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A6_panelA_`ssamp'.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear



***************************************************************
***************************************************************
***************************************************************
**  Panel B - Stayers by 2012 position 
***************************************************************
***************************************************************
***************************************************************
use zzfinish_del, clear
local wc "12"
keep if stay==1
gen zz = 0
replace zz = wagecat if year==2012
bys firm_tin tin: egen wagecat2012=sum(zz)
drop zz 
ren wagecat zwagecat
ren wagecat2012 wagecat
tab wagecat zwagecat
gen zz = 0
replace zz = wagecat if year==2012
bys firm_tin tin: egen wagecat2012=sum(zz)
drop zz 
local ssamp "stay12"

keep tin ft wages wages_ft  wages_tot wages_ft_tot firm_tin year wagecat
collapse (count) n_cat=tin (sum) n_catft=ft wages wages_ft (mean) wages_tot wages_ft_tot , by(firm_tin year wagecat)

sort firm_tin year
foreach o in wages wages_ft n_catft n_cat {
	forval i=1/`wc' {
		gen zz = 0
		replace zz = `o' if wagecat==`i'
		by firm_tin year: egen `o'`i'_`ssamp' = sum(zz)
		drop zz
	}
	by firm_tin year: egen `o'_tot_`ssamp' = sum(`o')
}
bys firm_tin year: gen nf=_n==1
tab year nf
keep if nf==1

sort firm_tin year
foreach o in wages wages_ft {
	forval i=1/`wc' {
		gen zz = 0 
		replace zz = `o'`i'_`ssamp' if year==2012
		by firm_tin: egen `o'`i'_`ssamp'_2012 = sum(zz)
		drop zz
	}
	gen zz = 0 
	replace zz = `o'_tot_`ssamp' if year==2012
	by firm_tin: egen `o'_`ssamp'_tot2012 = sum(zz)
	drop zz	
}
drop n_cat n_catft wages wages_ft wages_tot wages_ft_tot wagecat nf

*merge on distrib information from full sample
sort firm_tin year
merge 1:1 firm_tin year using $DATA/wage_distrib_all.dta
tab year _merge
gen flag_ind_`ssamp' = _merge==3
drop _merge

foreach vv in wages wages_ft n_cat n_catft {
forval i=1/12 {
	replace `vv'`i'_`ssamp' = 0 if `vv'`i'_`ssamp'==.
}
}

foreach vv in wages wages_ft {
forval i=1/12 {
	replace `vv'`i'_`ssamp'_2012 = 0 if `vv'`i'_`ssamp'_2012==.
}
}

foreach vv in n_cat_tot_`ssamp'  n_catft_tot_`ssamp'  wages_tot_`ssamp'  wages_ft_tot_`ssamp' wages_ft_`ssamp'_tot2012  wages_`ssamp'_tot2012 {
	replace `vv' = 0 if `vv'==.
}

* net shares
foreach o in wages_ft {
	forval i=1/`wc' {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

* create bins for deciles for main regs
forval j = 1/9 {
	gen dec`j'_`ssamp' = wages_ft`j'_`ssamp' 
	gen dec`j'_`ssamp'_2012 = wages_ft`j'_`ssamp'_2012 
}
egen dec10_`ssamp' = rowtotal(wages_ft10_`ssamp'-wages_ft12_`ssamp')
egen dec10_`ssamp'_2012 = rowtotal(wages_ft10_`ssamp'_2012-wages_ft12_`ssamp'_2012)
foreach o in dec {
	forval i=1/10 {
		gen dptn_`o'`i'_`ssamp' = (`o'`i'_`ssamp' - `o'`i'_`ssamp'_2012) / tot_net_iv_2012
	}
}

*************
**  Regressions
****************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.owntot_2012cat##ib2012.year i.naics_`baseyr'##ib`baseyr'.year i.state_2012##ib`baseyr'.year"

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t2011 t2015 t2016 y2011 y2015 y2016 `t'"

foreach r in 1 {
	forval j=1/9 {
		eststo r`r'_w`j': quietly areg dptn_dec`j'_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & n_ft_lower_2012>=15 , absorb(firm_tin) vce(cluster firm_tin)
	}
	eststo r`r'_w10: quietly areg dptn_dec10_`ssamp' `events' `r`r''  [aw=tot_pre_2012] if tot_pre_2012>0 & tt==0 & n_ft_lower_2012>=15, absorb(firm_tin) vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure_A6_panelB_`ssamp'.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear





* clean-up intermediate files
erase zzfinish_del.dta
erase $DATA/wage_distrib_all.dta






clear 
clear
exit
exit


